package org.summer.lawyer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.tree.Tree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.sysregion.SysRegionBo;
import org.summer.lawyer.domain.vo.sysregion.SysRegionVo;
import org.summer.lawyer.service.ISysRegionService;

import java.util.List;

/**
 * 【请填写功能名称】
 *
 * @author summer
 * @date 2024-06-28
 */
@Tag(name = "后台相关接口", description = "行政区域相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/sysRegion")
public class SysRegionController extends BaseController {

    private final ISysRegionService sysRegionService;

    /**
     * 查询【请填写功能名称】列表
     */
    @Operation(summary = "查询行政区域列表接口", description = "查询行政区域列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:region:list")
    @GetMapping("/list")
    public TableDataInfo<SysRegionVo> list(SysRegionBo bo, PageQuery pageQuery) {
        return sysRegionService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Operation(summary = "导出行政区域列表接口", description = "导出行政区域列表")
    @SaCheckPermission("lawyer:region:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysRegionBo bo, HttpServletResponse response) {
        List<SysRegionVo> list = sysRegionService.selectSysRegionVoList(bo);
        ExcelUtil.exportExcel(list, "行政区域", SysRegionVo.class, response);
    }

    /**
     * 获取【请填写功能名称】详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "获取【请填写功能名称】详细信息接口", description = "查询【请填写功能名称】列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:region:query")
    @GetMapping("/{id}")
    public R<SysRegionVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable String id) {
        return R.ok(sysRegionService.selectSysRegionVoById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Operation(summary = "新增【请填写功能名称】接口", description = "新增【请填写功能名称】", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:region:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveSysRegion")
    public R<Void> saveSysRegion(@Validated(AddGroup.class) @RequestBody SysRegionBo bo) {
        return toAjax(sysRegionService.insertByBo(bo));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Operation(summary = "修改【请填写功能名称】接口", description = "修改【请填写功能名称】", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:region:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateSysRegion")
    public R<Void> updateSysRegion(@Validated(EditGroup.class) @RequestBody SysRegionBo bo) {
        return toAjax(sysRegionService.updateByBo(bo));
    }

    /**
     * 删除【请填写功能名称】
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除【请填写功能名称】接口", description = "删除【请填写功能名称】", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:region:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeSysRegion/{ids}")
    public R<Void> removeSysRegion(@NotEmpty(message = "主键不能为空")
                                   @PathVariable String[] ids) {
        return toAjax(sysRegionService.deleteWithValidByIds(List.of(ids), true));
    }

    @Operation(summary = "获取全国行政区域数据")
    @SaIgnore
    @GetMapping("/regionTree")
    public R<List<Tree<String>>> regionTree(SysRegionBo bo){
        //bo.setPid("0");
        bo.setRegionLevel(2L);
        return R.ok(sysRegionService.selectRegionTreeList(bo));
    }

    @Operation(summary = "获取上海市辖区数据")
    @SaIgnore
    @GetMapping("/listDistrictShangHai")
    public R<List<SysRegionVo>> listShangHai(){
        return R.ok(sysRegionService.selectListDistrictByShangHai());
    }
}
